<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="12" :offset="6">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="form.type">
              <el-radio :label="1" :disabled="this.$route.params.id > 0"
                >目录</el-radio
              >
              <el-radio :label="2" :disabled="this.$route.params.id > 0"
                >菜单</el-radio>
              
            </el-radio-group>
          </el-form-item>

          <el-form-item label="上级" v-show="form.type == 2" prop="pid">
            <el-select v-model="form.pid">
              <el-option label="顶级目录" :value="0"></el-option>
              <el-option
                v-for="(item, index) in menuList"
                :key="index"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title"></el-input>
          </el-form-item>

          <el-form-item  label="图标" prop="icon">
            <el-input v-model="form.icon"></el-input>
          </el-form-item>

          <el-form-item v-show="form.status == 3" label="地址" prop="url">
            <el-input v-model="form.url"></el-input>
          </el-form-item>

          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio :label="3">正常</el-radio>
              <el-radio :label="4">禁用</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item>
            <el-button
              v-if="this.$route.params.id > 0"
              type="primary"
              @click="submitForm('form')"
              >立即添加</el-button
            >
            <el-button v-else type="primary" @click="submitForm('form')"
              >立即创建</el-button
            >
            <el-button @click="resetForm('form')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        type: 1,
        status: 3,
        icon: "",
        pid: 0,
        title: "",
        url: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入标题名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      menuList: [],
    };
  },
  created() {
    this.getSelectData();
    if (this.$route.params.id > 0) {
      this.getDataById(this.$route.params.id);
    }
  },
  methods: {
    // 获取下拉框数据
    getSelectData() {
      this.http
        .get("menulist?istree=1")
        .then((res) => {

          this.menuList = res.data.list;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getDataById(id) {
      this.http
        .get("/menuinfo", { params: { id } })
        .then((res) => {
          this.form = res.data.list;
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 先通过验证在发送请求
    submitForm(formName) {
      // this.$refs[formName]通过refs找到这个表单，然后验证
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.$route.params.id) {
            this.form.id=this.$route.params.id
            this.http
              .post("/menuedit", this.form)
              .then((res) => {
                console.log(res);
                this.$router.go(-1);
              })
              .catch((err) => {
                console.log(err);
              });
          } else {
            this.http
              .post("/menuadd", this.form)
              .then((res) => {
                console.log(res);
                this.$router.go(-1);
              })
              .catch((err) => {
                console.log(err);
              });
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>